package com.xiyi.shouxin.controller;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.ListSystemConfigParams;
import com.xiyi.shouxin.entity.admin.SystemConfig;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.SystemConfigService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.SystemConfigView;

@CrossOrigin("*")
@RequestMapping("/config")
@RestController
public class SystemConfigController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private SystemConfigService systemConfigService;

    @PostMapping("/list_config")
    public CommonModel listSystemConfig(@RequestBody ListSystemConfigParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            ItemPage<SystemConfigView> info = systemConfigService.findSystemConfigList(params.getConfigName(),
                    params.getPage(), params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询配置列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/update_config")
    public CommonModel updateSystemConfig(@RequestBody SystemConfig params) {
        if (params == null || params.getConfigName() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            systemConfigService.updateSystemConfig(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}修改配置{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/get_config")
    public CommonModel getConfig(@RequestBody SystemConfig params) {
        if (params == null || !StringUtils.hasText(params.getConfigName())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            SystemConfigView info = systemConfigService.findSystemConfig(params.getConfigName());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("查询配置{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @GetMapping("/get_config")
    public CommonModel getConfigForGet(@RequestParam String configName) {
        if ( !StringUtils.hasText(configName)) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            SystemConfigView info = systemConfigService.findSystemConfig(configName);
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("查询配置{}异常：", configName, e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
